package cn.keniykey.mc.landterritory.landflag.flag;

import java.util.Arrays;

import org.bukkit.entity.Enderman;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Silverfish;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityChangeBlockEvent;

import cn.keniykey.mc.landterritory.event.LandTerritoryReloadConfigEvent;
import cn.keniykey.mc.landterritory.land.api.ILandManager;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagParType;
import cn.keniykey.mc.landterritory.landflag.api.ELandFlagType;
import cn.keniykey.mc.landterritory.landflag.api.JavaLandFlag;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagConfigurationFile;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagListenerManager;
import cn.keniykey.mc.landterritory.toolkit.Util;

public final class BanEntityChangeFlag extends JavaLandFlag implements Listener{
	
	@Override
	public ELandFlagParType getELandFlagParType() {
		return ELandFlagParType.NONE;
	}
	@Override
	public ELandFlagType getELandFlagType() {
		return ELandFlagType.PLAYER;
	}
	@Override
	public boolean allowAllPlayerData() {
		return false;
	}
	
	@Override
	public void onEnable() {
		LandFlagListenerManager.registerListener(this, this);
	}
	@EventHandler
	public void onConfigLoad(final LandTerritoryReloadConfigEvent e) {
		final LandFlagConfigurationFile config = this.getLandFlagConfigurationFile();
		if(!config.has()) {
			config.set("name", "禁止生物修改方块");
			config.set("lore", Arrays.asList("&7（禁止蠹虫，末影人修改领地内的方块）"));
		}
		this.setDisplayName(Util.conver(config.getString("name")));
		this.setIntroductionLore(Util.conver(config.getStringList("lore")));
	}
	
	@EventHandler(priority = EventPriority.LOW,ignoreCancelled = true)
	public void onEntityChangeBlock(EntityChangeBlockEvent e) {
		final Entity entity = e.getEntity();
		if (!(entity instanceof Enderman) && !(entity instanceof Silverfish)) return;
		final Land land = ILandManager.getILandManager().getHighestPriorityLand(entity.getLocation());
		if(land != null && JavaLandFlag.hasLandFlag(land, this)) e.setCancelled(true);
	}
}